// app.js
'use strict';

const path = require('path');

module.exports = app => {
  // 在应用启动时执行的代码
  app.beforeStart(async () => {
    // 同步数据库模型
    // await app.model.sync();
    
    app.logger.info('小采灵犀智能对账系统启动完成');
  });
  
  // 添加响应辅助方法
  app.use(async (ctx, next) => {
    ctx.success = (data, message = '操作成功') => {
      ctx.body = {
        code: 0,
        data,
        message
      };
    };
    
    ctx.error = (message = '操作失败', code = 500) => {
      ctx.body = {
        code,
        message
      };
    };
    
    await next();
  });
  
  // 添加速率限制中间件
  const rateLimitMiddleware = require('./app/middleware/rate_limit_middleware.js');
  app.use(rateLimitMiddleware({}, app));
  
  // 添加JWT认证中间件
  const jwtAuthMiddleware = require('./app/middleware/jwt_auth_middleware.js');
  app.use(jwtAuthMiddleware({}, app));
  
  // 添加安全中间件
  const securityMiddleware = require('./app/middleware/security_middleware.js');
  app.use(securityMiddleware({}, app));
};